//
//  NIMSessionNotificationContentView.m
//  NIMKit
//
//  Created by chris on 15/3/9.
//  Copyright (c) 2015年 Netease. All rights reserved.
//

#import "NIMSessionNotificationContentView.h"
#import "NIMMessageModel.h"
#import "UIView+NIM.h"
#import "NIMKitUtil.h"
#import "UIImage+NIMKit.h"
#import "NIMKit.h"

@implementation NIMSessionNotificationContentView

- (instancetype)initSessionMessageContentView
{
    if (self = [super initSessionMessageContentView]) {
        _label = [[UILabel alloc] initWithFrame:CGRectZero];
        _label.numberOfLines = 0;
        _button = [[UIButton alloc] initWithFrame:CGRectZero];
        [_button setTitle:@"重新编辑" forState:UIControlStateNormal];
        [_button setTitleColor:[UIColor colorWithRed:0/255.0 green:118/255.0 blue:252/255.0 alpha:1.0] forState:UIControlStateNormal];
        [_button addTarget:self action:@selector(copyMessage) forControlEvents:UIControlEventTouchUpInside];
        _button.titleLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:_label];
        [self addSubview:_button];
    }
    return self;
}

- (void)refresh:(NIMMessageModel *)model
{
    [super refresh:model];
    self.label.text = [NIMKitUtil messageTipContent:model.message];
    NIMKitSetting *setting = [[NIMKit sharedKit].config setting:model.message];
    
    self.label.textColor = setting.textColor;
    self.label.font = setting.font;
    self.bubbleImageView.hidden = NO;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat padding = [NIMKit sharedKit].config.maxNotificationTipPadding;
    self.label.nim_size = [self.label sizeThatFits:CGSizeMake(self.nim_width - 2 * padding, CGFLOAT_MAX)];
    if ([self.label.text isEqualToString:@"你撤回了一条消息."]) {
        self.label.nim_centerX = self.nim_width * .5f;
        self.label.nim_centerY = self.nim_height * .5f;
        self.button.nim_size = CGSizeMake(50, 30);
        self.button.nim_left = self.label.nim_right + 10;
        self.button.nim_centerY = self.label.nim_centerY;
        self.button.hidden = NO;
        self.bubbleImageView.frame = CGRectInset(CGRectMake(self.label.frame.origin.x, self.label.frame.origin.y, self.label.frame.size.width + self.button.frame.size.width + 10, self.label.nim_size.height), -8, -4);
    } else {
        self.button.hidden = YES;
        self.label.nim_centerX = self.nim_width * .5f;
        self.label.nim_centerY = self.nim_height * .5f;
        self.bubbleImageView.frame = CGRectInset(self.label.frame, -8, -4);
    }
}

- (void)copyMessage {
    if (self.model.message.messageExt != @"" && self.model.message.messageExt != nil) {
        // 发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NIMSessionNotificationContentView" object:self.model.message.messageExt];
    }
}

@end
